package com.cyy.operation.appium;


import com.cyy.utils.Common;
import io.appium.java_client.android.nativekey.AndroidKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AndroidADB extends AbstractADB {

    @Autowired
    Common common;

    /**
     * 获取测试程序的 package
     *
     * @param apkpath apk安装包地址
     * @return
     */
    public String get_package(String apkpath) {
        String pkginfos = execmd("aapt dump badging " + apkpath + "| findstr package");
        String rgx = "name='(.*?)'";
        String pkg = common.findstr(pkginfos, rgx);
        return pkg.substring(5, pkg.length()).replace("'", "");

    }

    /**
     * 获取测试程序的 versionCode
     *
     * @param apkpath apk安装包地址
     * @return
     */
    public String get_versionCode(String apkpath) {
        String pkginfos = execmd("aapt dump badging " + apkpath + "| findstr package");
        String rgx = "versionCode='\\d+'";
        String pkg = common.findstr(pkginfos, rgx);
        return pkg.substring(12, pkg.length()).replace("'", "");
    }

    /**
     * 获取测试程序的 versionName
     *
     * @param apkpath apk安装包地址
     * @return
     */
    public String get_versionName(String apkpath) {
        String pkginfos = execmd("aapt dump badging " + apkpath + "| findstr package");
        String rgx = "versionName='(.*?)'";
        String pkg = common.findstr(pkginfos, rgx);
        return pkg.substring(12, pkg.length()).replace("'", "");
    }

    /**
     * 获取测试程序的 入口activity
     *
     * @param apkpath apk安装包地址
     * @return
     */
    public String get_activity(String apkpath) { //launchable-activity: name='com.cyyserver.SplashActivity'  label='濂藉笀鍌呮彺鍔㏒TAG' icon=''
        String pkginfos = execmd("aapt dump badging " + apkpath + "| findstr launchable-activity");
        String rgx = "name='(.*?)'";
        String pkg = common.findstr(pkginfos, rgx);
        return pkg.substring(5, pkg.length()).replace("'", "");
    }

    public void keyevent(AndroidKey keycode, String series) {
        exeadb("shell input keyevent " + keycode, series);
    }

    @Override
    public boolean call_phone(String series, String tel) {
        String scmd = exeadb(String.format("shell am start -a android.intent.action.CALL tel:%s", tel), series);
        return scmd.contains("Starting: Intent { act=android.intent.action.CALL dat=tel:xxxxxxxxxxx }");
    }

    @Override
    public void cancel_phone(String series) {
        exeadb("shell   input  keyevent  KEYCODE_ENDCALL", series);
        awake(series);
    }

    @Override
    public void send_message(String series, String tel, String msg) {
        exeadb(String.format("shell am start -a android.intent.action.SENDTO -d sms:%s --es sms_body \"%s\" --ez exit_on_sent true",tel,msg),series);
        exeadb("shell input keyevent 66",series); //模拟发送键
    }

    public static void main(String[] args) {
        AndroidADB adb = new AndroidADB();
//        adb.call_phone("192.168.6.246:6125","12345678902");
//        adb.sleep(3000);
//        adb.cancel_phone("192.168.6.246:6125");
        adb.send_message("192.168.6.246:6125","13798492797","自动化测试调试");
    }
}
